<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="交易订单号" prop="outTradeNo">
        <el-input v-model="queryParams.outTradeNo" placeholder="请输入交易订单号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="总金额" prop="totalAmount">
        <el-input v-model="queryParams.totalAmount" placeholder="请输入总金额" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="商品名字" prop="subject">
        <el-input v-model="queryParams.subject" placeholder="请输入商品名字" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="交易状态" prop="tradeStatus">
        <el-select v-model="queryParams.tradeStatus" placeholder="请选择交易状态" clearable>
          <el-option v-for="dict in dict.order_trade_status" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="折扣金额" prop="discountAmount">
        <el-input v-model="queryParams.discountAmount" placeholder="请输入折扣金额" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker clearable v-model="queryParams.createTime" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="支付成功时间" prop="successTime">
        <el-date-picker clearable v-model="queryParams.successTime" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择支付成功时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="订单关闭时间" prop="closeTime">
        <el-date-picker clearable v-model="queryParams.closeTime" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择订单关闭时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" size="mini" @click="handleAdd"
          v-hasPermi="['zheyin-pay:table:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['zheyin-pay:table:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['zheyin-pay:table:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" size="mini" @click="handleExport"
          v-hasPermi="['zheyin-pay:table:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="tableList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单id" align="center" prop="id" />
      <el-table-column label="交易订单号" align="center" prop="outTradeNo" />
      <el-table-column label="总金额" align="center" prop="totalAmount" />
      <el-table-column label="商品名字" align="center" prop="subject" />
      <el-table-column label="交易状态" align="center" prop="tradeStatus">
        <template #default="scope">
          <dict-tag :options="dict.order_trade_status" :value="scope.row.tradeStatus" />
        </template>
      </el-table-column>
      <el-table-column label="支付成功时间" align="center" prop="successTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.successTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="订单关闭时间" align="center" prop="closeTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.closeTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button size="mini" type="text" icon="Edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['zheyin-pay:table:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="Delete" @click="handleDelete(scope.row)"
            v-hasPermi="['zheyin-pay:table:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改订单信息对话框 -->
    <el-dialog :title="title" v-model="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-form-item label="交易订单号" prop="outTradeNo">
          <el-input v-model="form.outTradeNo" placeholder="请输入交易订单号" />
        </el-form-item>
        <el-form-item label="总金额" prop="totalAmount">
          <el-input v-model="form.totalAmount" placeholder="请输入总金额" />
        </el-form-item>
        <el-form-item label="商品名字" prop="subject">
          <el-input v-model="form.subject" placeholder="请输入商品名字" />
        </el-form-item>
        <el-form-item label="商品信息" prop="subjectInfo">
          <el-input v-model="form.subjectInfo" type="textarea" placeholder="请输入内容" />
        </el-form-item>

        <el-form-item label="交易状态" prop="tradeStatus">
          <el-select v-model="form.tradeStatus" placeholder="请选择交易状态">
            <el-option v-for="dict in dict.order_trade_status" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="原始金额" prop="orginAmount">
          <el-input v-model="form.orginAmount" placeholder="请输入原始金额" />
        </el-form-item>
        <el-form-item label="享受折扣" prop="discount">
          <el-input v-model="form.discount" placeholder="请输入享受折扣" />
        </el-form-item>
        <el-form-item label="折扣金额" prop="discountAmount">
          <el-input v-model="form.discountAmount" placeholder="请输入折扣金额" />
        </el-form-item>
        <el-form-item label="支付成功时间" prop="successTime">
          <el-date-picker clearable v-model="form.successTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择支付成功时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="订单关闭时间" prop="closeTime">
          <el-date-picker clearable v-model="form.closeTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择订单关闭时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTable, getTable, delTable, addTable, updateTable } from "@/api/order/table";
export default {
  name: "Table",
  data() {
    return {
      dict: {
        order_trade_status: null
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单信息表格数据
      tableList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        outTradeNo: null,
        totalAmount: null,
        subject: null,
        tradeStatus: null,
        discountAmount: null,
        createTime: null,
        successTime: null,
        closeTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        outTradeNo: [
          { required: true, message: "交易订单号不能为空", trigger: "blur" }
        ],
        totalAmount: [
          { required: true, message: "总金额不能为空", trigger: "blur" }
        ],
        subject: [
          { required: true, message: "商品名字不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  mounted() {
    const { order_trade_status } = this.useDict(['order_trade_status']);
    this.dict.order_trade_status = order_trade_status
  },
  methods: {
    /** 查询订单信息列表 */
    getList() {
      console.log(this.dict)
      this.loading = true;
      listTable(this.queryParams).then(response => {
        this.tableList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        outTradeNo: null,
        totalAmount: null,
        subject: null,
        subjectInfo: null,
        tradeStatus: null,
        orginAmount: null,
        discount: null,
        discountAmount: null,
        createBy: null,
        createTime: null,
        successTime: null,
        updateBy: null,
        updateTime: null,
        closeTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getTable(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改订单信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateTable(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTable(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除订单信息编号为"' + ids + '"的数据项？').then(function () {
        return delTable(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('zheyin-pay/table/export', {
        ...this.queryParams
      }, `table_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
